package com.enjoy.ui;

import java.util.Calendar;

import org.json.JSONException;
import org.json.JSONObject;

import com.enjoy.R;
import com.enjoy.commons.Commons;
import com.enjoy.utils.HttpPostHandle;
import com.enjoy.utils.PreferenceHelper;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

public class CreateEvent extends Activity{
	
	private Button register,cancel;
	private EditText eventname,eventcontent,eventduration,eventtab,eventpalcename;
	private int year,month,day,hour,minute;
	private Calendar c;
	private DatePicker dpicker;  
    private TimePicker tpicker;  
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_event);
		final String longitude=this.getIntent().getExtras().getString("longitude");
		final String latitude=this.getIntent().getExtras().getString("latitude");
		final String placename=this.getIntent().getExtras().getString("placename");
		
		 register=(Button)findViewById(R.id.register);
		 cancel=(Button)findViewById(R.id.cancel);
		 eventname = (EditText)findViewById(R.id.eventname);
	     eventcontent = (EditText)findViewById(R.id.eventcontent);
	     eventduration = (EditText)findViewById(R.id.eventduration);
	     eventpalcename =(EditText)findViewById(R.id.eventplacename);
	     eventtab = (EditText)findViewById(R.id.eventtab);
	     dpicker = (DatePicker)findViewById(R.id.dPicker);
	     tpicker =(TimePicker)findViewById(R.id.tPicker);
	   
	     eventpalcename.setText(placename);
	     eventpalcename.setKeyListener(null);
	     
	     
	     c = Calendar.getInstance();
	        
	     year = c.get(Calendar.YEAR);
	     month = c.get(Calendar.MONTH);
	     day = c.get(Calendar.DAY_OF_MONTH);  
	     hour = c.get(Calendar.HOUR);
	     minute = c.get(Calendar.MINUTE);
	     dpicker.init(year, month, day, null);
	     dpicker.setCalendarViewShown(false);
	     tpicker.setIs24HourView(true);// 设置是否为24小时制  
	     tpicker.setCurrentHour(hour);  
	     tpicker.setCurrentMinute(minute);  
	     
	     register.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
	                boolean success=true;
					HttpPostHandle handle = new HttpPostHandle(Commons.URL
							+ "serviceName=EventService&operation=insert");
					
					if(eventname.getText().toString().equals("")
							||eventcontent.getText().toString().equals("")
							||eventduration.getText().toString().equals("")
							||eventtab.getText().toString().equals(""))
					{
						success=false;
						Toast.makeText(CreateEvent.this, "请完善信息",
								Toast.LENGTH_LONG).show();
					}
					else
					{
						
						handle.add("uid",PreferenceHelper.getSetting(CreateEvent.this, "uid","0"));
						handle.add("longitude",  longitude);
						handle.add("latitude", latitude);
						handle.add("placename", placename);
					    handle.add("ename",eventname.getText().toString() );
					    handle.add("content",eventcontent.getText().toString());
					    handle.add("duration",eventduration.getText().toString());
					    handle.add("status",eventtab.getText().toString());
					    
					    String stime=dpicker.getYear()+"-"+dpicker.getMonth()+"-"+dpicker.getDayOfMonth()+" ";
					    if(tpicker.getCurrentHour()-10<0)
					    	stime=stime+"0"+tpicker.getCurrentHour()+":";
					    else {
							stime=stime+tpicker.getCurrentHour()+":";
						}
					    
					    if(tpicker.getCurrentMinute()-10<0){
					    	stime=stime+"0"+tpicker.getCurrentMinute();
					    }else {
							stime=stime+tpicker.getCurrentMinute();;
						}
					    
					    handle.add("stime", stime);
					 
					
					}
					
					
					try {
						JSONObject JSON;
						if(success==true)
						{
							JSON = handle.getResponse();
						if(JSON.getInt("errorcode")==0)
						{
							Toast.makeText(CreateEvent.this, JSON.getString("errormessage"),
									Toast.LENGTH_LONG).show();
						}
						else
						{
							Toast.makeText(CreateEvent.this, "创建失败",
									Toast.LENGTH_LONG).show();
						}}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			});
		
	     
	     
	     
	}

}
